//https://leetcode.cn/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/solution/mian-shi-ti-09-yong-liang-ge-zhan-shi-xian-dui-l-3/


//栈：先进后出，
//队列：先进先出
//这里需要注意的是当删除头部时，不需要回倒，等下个插入时才回倒
//思考点：
/** 
 * 1.刚开始，删除完之后就回倒，效率差（如果连续删除会有多余倒来倒去，n^2次）
 * 2.第一次优化，删除完后不回倒，等插入时再回倒，效率不好 (n次)
 * 3.根本就不需要回倒，只需要判断stackb有没有，有则直接Pop，没有才从A导入（没有多余的操作）
 * **/

var CQueue = function() {
    this.inStack = [];
    this.outStack = [];
};

/** 
 * @param {number} value
 * @return {void}
 */
CQueue.prototype.appendTail = function(value) {
    this.inStack.push(value);
};

/**
 * @return {number}
 */
CQueue.prototype.deleteHead = function() {
    if (!this.outStack.length) {
        if (!this.inStack.length) {
            return -1;
        }
        while (this.inStack.length) {
            this.outStack.push(this.inStack.pop());
        }
    }
    return this.outStack.pop();
};

/**
 * Your CQueue object will be instantiated and called as such:
 * var obj = new CQueue()
 * obj.appendTail(value)
 * var param_2 = obj.deleteHead()
 */

/**
 * Your CQueue object will be instantiated and called as such:
 * var obj = new CQueue()
 * obj.appendTail(value)
 * var param_2 = obj.deleteHead()
 */
console.log(CQueue,typeof(CQueue))
var obj = new CQueue()
console.log(clone(obj))
console.log(obj.deleteHead())
console.log(clone(obj))
obj.appendTail(1)
console.log(clone(obj))
obj.appendTail(2)
console.log(clone(obj));
var param_2 = obj.deleteHead()
console.log(clone(obj));
obj.appendTail(3)
obj.appendTail(4)
console.log(clone(obj));
//待优化，是否有必要每次都回倒


function clone(obj){
    return JSON.parse(JSON.stringify(obj))
}